import { _decorator, Button, director, Label, Node, Sprite } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { GameData } from '../../Data/GameData';
import { EventMsgKes } from '../../Config/ResConfig';
import { GameTip } from '../../../../Framework/Scripts/Managers/TipMgr';
import { MsgData } from '../../Data/MsgData';
import { SceneMgr } from '../../../../Framework/Scripts/Managers/SceneMgr';
import { Platform } from '../../../../Framework/Scripts/Platform/PlatformWX';
import { WECHAT } from 'cc/env';
import { IUserInfo } from '../../../AssetPackage/Props/Hall/Hall';
import { FriendJoinBroadcast, FriendStartPKRes } from '../../../AssetPackage/Props/Game/DT_PK';
const { ccclass, property } = _decorator;

@ccclass('GameHY_Ctr')
export class GameHY_Ctr extends UIComponent {
    labDes: any;
    nLeft: Node;
    nRight: Node;

    isJoin: boolean;
    isClick: boolean;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
        this.setBgAni(0);
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.labDes = this.ViewComponent('Label',Label);
        this.nLeft = this.ViewNode('node/spL');
        this.nRight = this.ViewNode('node/spR');

        this.AddButtonListener('btnBack',this,this.onBtnCloseClick1);
        this.AddButtonListener('btn1',this,this.onBtnYQClick);
        this.AddButtonListener('btn2',this,this.onBtnStartPKClick);
    }

    private InitData(){
        this.isJoin = false;
        this.isClick = true;
        //设计自己的数据
        // this.setInfoData(this.nLeft,GameData.inst.userInfo);
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgFriendJoinBroadcast,this.onFriendJoinBroadcast,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgFriendStartPKRes,this.onFriendStartPKRes,this);
        
    }

    private unRegisterEvent(){
        globalThis.gameMgr.off(EventMsgKes.msgFriendJoinBroadcast,this.onFriendJoinBroadcast,this);
        globalThis.gameMgr.off(EventMsgKes.msgFriendStartPKRes,this.onFriendStartPKRes,this);
    }

    private onBtnCloseClick1(){
        SceneMgr.inst.showHall();
    }

    private setLabDes(total: number, times: number){
        this.labDes.string = `共${total}题，${times}秒回答时间`;
    }

    private setInfoData(node:Node, user: IUserInfo){
        let labName = node.getChildByPath('sp/Label').getComponent(Label);
        let sphead=  node.getChildByPath('head/spHead').getComponent(Sprite);
        this.setHeadMat(sphead);
        if(user.avator){
            this.LoadRemoteImage(user.avator, sphead);
        }
        if(user.name){
            labName.string = user.name; 
        }
    }

    //邀请好友
    private onBtnYQClick(btn:Button) {
        if(WECHAT){
            //测试微信分享带参数
            let str = {
                userId: 10001,
                roomId: 1000
            }
            console.log('=======onBtnYQClick=========',str);
            Platform.inst.shareFriendToJoin(JSON.stringify(str));
        }else{
            GameTip.inst.showTip('当前不是微信环境！');
        }
    }

    private onBtnStartPKClick(btn:Button) {
        if(this.isJoin == false){
            GameTip.inst.showTip('当前有玩家未加入!');
            return;
        }
        if(this.isClick == false){
            return;
        }
        this.isClick = false;

        globalThis.gameMgr._GameHallMsg.FriendStartPKReq();
    }

    //有玩家加入广播成功
    private onFriendJoinBroadcast(data:FriendJoinBroadcast ){
        console.log('==========onFriendJoinBroadcast=========',data);
        let user = data.user;
        if(user){
            this.setInfoData(this.nRight,user);
            this.isJoin = true;
        }
    }

    //开始pk返回
    private onFriendStartPKRes(data:FriendStartPKRes){
        console.log('==========onFriendStartPKRes=========',data);
        let code = data.code || 0;
        if(code == 0){
            //进入游戏场景
            SceneMgr.inst.goToGame('Prefabs/GamePlay/GamePlay',(ts_node)=>{
                ts_node.setGameType(2);
                this.node.destroy();
            })
        }
    }
}


